/**
 * 
 */
package hcmus.fit.master.sevensea.view;

import hcmus.fit.master.sevensea.bus.BlackPirate;
import hcmus.fit.master.sevensea.bus.GoodShip;
import hcmus.fit.master.sevensea.bus.Sea;
import hcmus.fit.master.sevensea.core.AbstractView;
import hcmus.fit.master.sevensea.core.Sprite;
import hcmus.fit.master.sevensea.utils.GameUtils;
import hcmus.fit.master.sevensea.utils.Size;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap.Config;
import android.view.MotionEvent;

/**
 * @author hvp
 *
 */
public class PlayView extends AbstractView {

    private List<Sprite> sprites;
    private Bitmap bufferBitmap;
    public PlayView(Context context) {
        super(context);
        sprites = new ArrayList<Sprite>();
        Size size = GameUtils.getScreenSize(context);
//        System.out.println(size.getWidth());
//        System.out.println(size.getHeight());
//        int t = Math.min(size.getWidth(), size.getHeight());
        // Create sea map
        bufferBitmap = Bitmap.createBitmap(size.getWidth(), size.getHeight(), Config.ARGB_8888);
        sprites.add(new Sea(getResources(), size));
        // Create main ship
        sprites.add(new GoodShip(getResources(), new Point(5, 5)));
        // Create black ship
        sprites.add(new BlackPirate(getResources(), new Point(7, 7)));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Canvas c = new Canvas(bufferBitmap);
        super.onDraw(c);
        for (Sprite sprite : sprites) {
            sprite.draw(c);
        }
        canvas.drawBitmap(bufferBitmap, 0, 0, null);
    }

    @Override
    public void update() {
        for (Sprite sprite : sprites) {
            sprite.update();
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
     // TODO Auto-generated method stub
        if (event.getAction() == MotionEvent.ACTION_UP) {
//            int idx = SelectedSpritedIndex(event.getX(), event.getY());
        }
        return super.onTouchEvent(event);
    }
}
